1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file cpp_type_traits.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
33 #ifndef _CPP_TYPE_TRAITS_H
34 #define _CPP_TYPE_TRAITS_H 1
36 #pragma GCC system_header
38 #include <bits/c++config.h>
41 // This file provides some compile-time information about various types.
42 // These representations were designed, on purpose, to be constant-expressions
43 // and not types as found in <bits/type_traits.h>. In particular, they
44 // can be used in control structures and the optimizer hopefully will do
47 // Why integral expressions, and not functions nor types?
48 // Firstly, these compile-time entities are used as template-arguments
49 // so function return values won't work: We need compile-time entities.
50 // We're left with types and constant integral expressions.
51 // Secondly, from the point of view of ease of use, type-based compile-time
52 // information is -not- *that* convenient. On has to write lots of
53 // overloaded functions and to hope that the compiler will select the right
54 // one. As a net effect, the overall structure isn't very clear at first
56 // Thirdly, partial ordering and overload resolution (of function templates)
57 // is highly costly in terms of compiler-resource. It is a Good Thing to
58 // keep these resource consumption as least as possible.
60 // See valarray_array.h for a case use.
62 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
64 // Update 2005: types are also provided and <bits/type_traits.h> has been
68 // Forward declaration hack, should really include this from somewhere.
69 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
71 template<typename _Iterator, typename _Container>
72 class __normal_iterator;
74 _GLIBCXX_END_NAMESPACE
76 _GLIBCXX_BEGIN_NAMESPACE(std)
78 struct __true_type { };
79 struct __false_type { };
83 { typedef __false_type __type; };
86 struct __truth_type<true>
87 { typedef __true_type __type; };
89 // N.B. The conversions to bool are needed due to the issue
90 // explained in c++/19404.
91 template<class _Sp, class _Tp>
94 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
95 typedef typename __truth_type<__value>::__type __type;
98 // Compare for equality of types.
99 template<typename, typename>
102 enum { __value = 0 };
103 typedef __false_type __type;
106 template<typename _Tp>
107 struct __are_same<_Tp, _Tp>
109 enum { __value = 1 };
110 typedef __true_type __type;
113 // Holds if the template-argument is a void type.
114 template<typename _Tp>
117 enum { __value = 0 };
118 typedef __false_type __type;
122 struct __is_void<void>
124 enum { __value = 1 };
125 typedef __true_type __type;
131 template<typename _Tp>
134 enum { __value = 0 };
135 typedef __false_type __type;
138 // Thirteen specializations (yes there are eleven standard integer
139 // types; 'long long' and 'unsigned long long' are supported as
142 struct __is_integer<bool>
144 enum { __value = 1 };
145 typedef __true_type __type;
149 struct __is_integer<char>
151 enum { __value = 1 };
152 typedef __true_type __type;
156 struct __is_integer<signed char>
158 enum { __value = 1 };
159 typedef __true_type __type;
163 struct __is_integer<unsigned char>
165 enum { __value = 1 };
166 typedef __true_type __type;
169 # ifdef _GLIBCXX_USE_WCHAR_T
171 struct __is_integer<wchar_t>
173 enum { __value = 1 };
174 typedef __true_type __type;
178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
180 struct __is_integer<char16_t>
182 enum { __value = 1 };
183 typedef __true_type __type;
187 struct __is_integer<char32_t>
189 enum { __value = 1 };
190 typedef __true_type __type;
195 struct __is_integer<short>
197 enum { __value = 1 };
198 typedef __true_type __type;
202 struct __is_integer<unsigned short>
204 enum { __value = 1 };
205 typedef __true_type __type;
209 struct __is_integer<int>
211 enum { __value = 1 };
212 typedef __true_type __type;
216 struct __is_integer<unsigned int>
218 enum { __value = 1 };
219 typedef __true_type __type;
223 struct __is_integer<long>
225 enum { __value = 1 };
226 typedef __true_type __type;
230 struct __is_integer<unsigned long>
232 enum { __value = 1 };
233 typedef __true_type __type;
237 struct __is_integer<long long>
239 enum { __value = 1 };
240 typedef __true_type __type;
244 struct __is_integer<unsigned long long>
246 enum { __value = 1 };
247 typedef __true_type __type;
251 // Floating point types
253 template<typename _Tp>
256 enum { __value = 0 };
257 typedef __false_type __type;
260 // three specializations (float, double and 'long double')
262 struct __is_floating<float>
264 enum { __value = 1 };
265 typedef __true_type __type;
269 struct __is_floating<double>
271 enum { __value = 1 };
272 typedef __true_type __type;
276 struct __is_floating<long double>
278 enum { __value = 1 };
279 typedef __true_type __type;
285 template<typename _Tp>
288 enum { __value = 0 };
289 typedef __false_type __type;
292 template<typename _Tp>
293 struct __is_pointer<_Tp*>
295 enum { __value = 1 };
296 typedef __true_type __type;
300 // Normal iterator type
302 template<typename _Tp>
303 struct __is_normal_iterator
305 enum { __value = 0 };
306 typedef __false_type __type;
309 template<typename _Iterator, typename _Container>
310 struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
313 enum { __value = 1 };
314 typedef __true_type __type;
318 // An arithmetic type is an integer type or a floating point type
320 template<typename _Tp>
321 struct __is_arithmetic
322 : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
326 // A fundamental type is `void' or and arithmetic type
328 template<typename _Tp>
329 struct __is_fundamental
330 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
334 // A scalar type is an arithmetic type or a pointer type
336 template<typename _Tp>
338 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
342 // For use in std::copy and std::find overloads for streambuf iterators.
344 template<typename _Tp>
347 enum { __value = 0 };
348 typedef __false_type __type;
352 struct __is_char<char>
354 enum { __value = 1 };
355 typedef __true_type __type;
358 #ifdef _GLIBCXX_USE_WCHAR_T
360 struct __is_char<wchar_t>
362 enum { __value = 1 };
363 typedef __true_type __type;
367 template<typename _Tp>
370 enum { __value = 0 };
371 typedef __false_type __type;
375 struct __is_byte<char>
377 enum { __value = 1 };
378 typedef __true_type __type;
382 struct __is_byte<signed char>
384 enum { __value = 1 };
385 typedef __true_type __type;
389 struct __is_byte<unsigned char>
391 enum { __value = 1 };
392 typedef __true_type __type;
396 // Move iterator type
398 template<typename _Tp>
399 struct __is_move_iterator
401 enum { __value = 0 };
402 typedef __false_type __type;
405 #ifdef __GXX_EXPERIMENTAL_CXX0X__
406 template<typename _Iterator>
409 template<typename _Iterator>
410 struct __is_move_iterator< move_iterator<_Iterator> >
412 enum { __value = 1 };
413 typedef __true_type __type;
417 _GLIBCXX_END_NAMESPACE
419 #endif //_CPP_TYPE_TRAITS_H